#练习题

#1-建立银行账户
class BankAccount:
    #属性名称添加两个下划线表示私有属性
    __tip=''
    def __init__(self,accountName,accountNumber):
        self.accountName=accountName
        self.accountNumber=accountNumber
        self.__balance=0.00

    #获取余额
    def GetBalance(self):
        return self.__balance

    #存钱
    def AddMoney(self,curAddMoney):
        if curAddMoney>0:
            self.__balance+=curAddMoney
        
    #取钱
    def GetMoney(self,needGetMoney):
        if needGetMoney>0:
            if needGetMoney<=self.__balance:
               self.__tip=''
               self.__balance-=needGetMoney
            else:
                self.__tip='取款金额大于当前余额，无法取款，请重新输入取款金额'
            

    def __str__(self):
        if self.__tip!='':
            msg='账号：'+str(self.accountNumber)+'\n'+'账户名：'+str(self.accountName)+'\n'+self.__tip+" \n"+'当前余额：'+str(self.__balance)
        else:
            msg='账号：'+str(self.accountNumber)+'\n'+'账户名：'+str(self.accountName)+'\n'+'当前余额：'+str(self.__balance)
        return msg
        

print("\n测试银行账户")
accountName='张三'
accountNumber='6220301257641257'
newBankAccount=BankAccount(accountName,accountNumber)
##私有属性只能在类里面使用，外面无法使用
# newBankAccount.__balance=2000
# newBankAccount.__tip='初始化'
print(newBankAccount)
saveMoney=300
print('\n向当前的账号：',accountNumber,'存入%f元钱'%saveMoney)
newBankAccount.AddMoney(saveMoney)
print(newBankAccount)
saveMoney2=126
print('\n向当前的账号：',accountNumber,'存入%f元钱'%saveMoney2)
newBankAccount.AddMoney(saveMoney2)
print(newBankAccount)
getMoney=600
print('\n向当前的账号：',accountNumber,'取%f元钱'%getMoney)
newBankAccount.GetMoney(getMoney)
print(newBankAccount)
    

#2-建立一个可以铮利息的类
class InterestAccount(BankAccount):
    #利息比例
    __interestRate=0.0275
    #利息
    __interest=0.00
    #本金和利息
    __MoneyAndInterest=0.00

    def __init__(self, accountName, accountNumber):
        super().__init__(accountName, accountNumber)
    
    #调整利率
    def adjustInterestRate(self,curInterestRate):
        if curInterestRate>0:
            self.__interestRate=curInterestRate

    #获取利息
    def GetInterest(self,balance):
        if balance>0:
            print("当前金额为：",balance)
            self.__interest = self.__interestRate*balance
            return self.__interest
    
    #获取本息
    def GetMoneyAndInterest(self,balance):
        self.__MoneyAndInterest=balance+self.GetInterest(balance)
        return self.__MoneyAndInterest
    

    def __str__(self):
        msg="\n当前的利率是："+str(self.__interestRate)+"\n当前的利息是："+str(self.__interest)+"\n"
        return msg

    
print('\n\n测试利息')
testInterest=InterestAccount(accountName,accountNumber)
print(testInterest)
print('\n获取利息')
testInterest.GetInterest(newBankAccount.GetBalance())
print(testInterest)
print('\n开始调整利率为0.03')
testInterest.adjustInterestRate(0.03)
print('\n获取本息')
testInterest.GetMoneyAndInterest(newBankAccount.GetBalance())
print(testInterest)



saveMoney3=666
print('\n向当前的账号：',accountNumber,'存入%f元钱'%saveMoney3)
newBankAccount.AddMoney(saveMoney3)
print(newBankAccount)
print('\n获取利息')
testInterest.GetInterest(newBankAccount.GetBalance())
print(testInterest)
print('\n获取本息')
testInterest.GetMoneyAndInterest(newBankAccount.GetBalance())
print(testInterest)

getMoney=600
print('\n向当前的账号：',accountNumber,'取%f元钱'%getMoney)
newBankAccount.GetMoney(getMoney)
print(newBankAccount)

